<template>
	<view class="container" style="padding-bottom: 120rpx;">
		<u--form ref="queryForm" :model="query">
			<u-form-item label="" prop="containerCode" style="display: flex;">
				<span style="margin-right: 20rpx;">容器码</span>
				<u--input
					placeholder="请输入/扫描"
					v-model="query.containerCode"
					suffixIcon="scan"
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
					@confirm="getDetail"
				>
				</u--input>
			</u-form-item>
			<view style="display: flex;flex-direction: column;">
				<!-- 一格、两格 -->
				<view v-if="isolationArea === 1 || isolationArea === 2" style="display: flex;flex-direction: column;margin-top: 20rpx;gap: 20rpx;width: 95%;margin: 10rpx auto">
					<view v-for="(item, index) in list" :key="index">
						<view style="display: flex;height: 200rpx;">
							<view v-if="item.isAllow === 0" style="padding-left: 100rpx;border: #f56c6c 2rpx solid;font-size: 30rpx;flex-direction: column;flex: 8;background-color: #f7b2b2;display: flex;align-items: center;justify-content: center;">
								<view style="display: flex;width: 100%;">
									<view style="flex: 3;font-size: 30rpx;">物料:</view>
									<view style="flex: 2;font-size: 30rpx;">{{ item.materialCode || '无' }}</view>
								</view>
								<view style="display: flex;width: 100%;">
									<view style="flex: 3;font-size: 30rpx;">批次号:</view>
									<view style="flex: 2;font-size: 30rpx;">{{ item.materialBatch || '无' }}</view>
								</view>
								<view style="display: flex;width: 100%;align-items: center;">
									<view style="flex: 3;font-size: 30rpx;">数量:</view>
									<view style="flex: 2;font-size: 30rpx;">{{ item.preQty || 0 }}</view>
								</view>
							</view>
							<view v-else style="padding-left: 100rpx;border: #53c21c 2rpx solid;font-size: 30rpx;flex-direction: column;flex: 8;background-color: #a9e08f;display: flex;align-items: center;justify-content: center;">
								<view style="display: flex;width: 100%;">
									<view style="flex: 3;font-size: 30rpx;">物料:</view>
									<view style="flex: 2;font-size: 30rpx;">{{ item.materialCode }}</view>
								</view>
								<view style="display: flex;width: 100%;">
									<view style="flex: 3;font-size: 30rpx;">批次号:</view>
									<view style="flex: 2;font-size: 30rpx;">{{ item.materialBatch || '无' }}</view>
								</view>
								<view style="display: flex;width: 100%;align-items: center;">
									<view style="flex: 3;font-size: 30rpx;">数量:</view>
									<view class="quantity" style="flex: 2;font-size: 30rpx;">
										<u--input
											type="number"
											:placeholder="String(item.preQty)"
											border="surround"
											v-model="item.quantity"
											style="background-color: #fff;width: 100rpx;"
										></u--input>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<!-- 四格、八格 -->
				<view v-else style="display: flex;flex-wrap: wrap;justify-content: space-around;">
					<view v-for="(item, index) in list" :key="index" style="margin-top: 20rpx;width: 45%;box-sizing: border-box;">
						<view style="display: flex;height: 200rpx;">
							<view v-if="item.isAllow === 0" style="padding: 15rpx; border: #f56c6c 2rpx solid;font-size: 26rpx;flex-direction: column;flex: 5;background-color: #f7b2b2;display: flex;align-items: center;justify-content: center;">
								<view style="display: flex;width: 100%;">
									<view style="flex: 3;font-size: 30rpx;">物料:</view>
									<view style="flex: 2;font-size: 30rpx;">{{ item.materialCode || '无' }}</view>
								</view>
								<view style="display: flex;width: 100%;">
									<view style="flex: 3;font-size: 30rpx;">批次号:</view>
									<view style="flex: 2;font-size: 30rpx;">{{ item.materialBatch || '无' }}</view>
								</view>
								<view style="display: flex;width: 100%;align-items: center;">
									<view style="flex: 3;font-size: 30rpx;">数量:</view>
									<view style="flex: 2;font-size: 30rpx;">{{ item.preQty || 0 }}</view>
								</view>
							</view>
							<view v-else style="box-sizing: border-box;padding: 15rpx; border: #53c21c 2rpx solid;font-size: 26rpx;flex-direction: column;flex: 5;background-color: #a9e08f;display: flex;align-items: center;justify-content: center;">
								<view style="display: flex;width: 100%;">
									<view style="flex: 3;font-size: 30rpx;">物料:</view>
									<view style="flex: 2;font-size: 30rpx;">{{ item.materialCode }}</view>
								</view>
								<view style="display: flex;width: 100%;">
									<view style="flex: 3;font-size: 30rpx;">批次号:</view>
									<view style="flex: 2;font-size: 30rpx;">{{ item.materialBatch || '无' }}</view>
								</view>
								<view style="display: flex;width: 100%;align-items: center;">
									<view style="flex: 3;font-size: 30rpx;">数量:</view>
									<view class="quantity" style="flex: 2;font-size: 30rpx;">
										<u--input
											:placeholder="String(item.preQty)"
											border="surround"
											v-model="item.quantity"
											style="background-color: #fff;width: 100rpx;"
										></u--input>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</u--form>
		<view style="position: fixed; bottom: 0; left: 0; width: 100%; padding: 10rpx; background-color: #fff; display: flex; justify-content: space-between;box-sizing: border-box;gap: 20rpx;">
			<u-button type="primary" text="分拣完成" @click="submit"></u-button>
			<u-button type="warning" text="重置" @click="reset"></u-button>
			<!-- <u-button type="success" text="打印" @click="print"></u-button> -->
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import { getDetailAPI, confirmSortAPI } from '@/api/work/inbound.js'
	import uniDataSelect from '@dcloudio/uni-ui/lib/uni-data-select/uni-data-select.vue';
	export default {
		components: {
			uniDataSelect
		},
		data() {
			return {
				query: {
					containerCode: ''
				},
				list: [],
				isolationArea: 0,
				flag: false
			};
		},
		onLoad(options) {
			uni.$on('scancodedate',(data) => this.getCode(data.code))
			if (options.containerCode) {
				this.query.containerCode = options.containerCode
				this.getDetail()
				this.flag = true
			}
		},
		onUnload() {  
		  // 移除监听事件      
		  uni.$off('scancodedate')
		},
		methods: {
			// 扫码
			getCode(code) {
				const pattern = /^[A-Za-z]\d{9}$/
				if (pattern.test(code)) {
					this.query.containerCode = code
					this.getDetail()
				}
			},
			async submit() {
				const submitObject = {
					containerDetailDtos: this.list
				}
				for (let item of submitObject.containerDetailDtos) {
				  // 如果 isAllow 为 1，检查 quantity 是否有值
				  if (item.isAllow === 1 && (item.quantity === null || item.quantity === undefined || item.quantity === '')) {
				    // 如果 quantity 没有值，跳出方法
				    return this.$refs.uToast.show({
							type: 'warning',
							message: '必须填写数量'
						})
				  }
					if (!/^\d+$/.test(item.quantity)) {
						return this.$refs.uToast.show({
							type: 'warning',
							message: '数量必须为数字且大于等于0'
						})
					}
				}
				const res = await confirmSortAPI(submitObject)
				if (res.meta.code === 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: res.meta.msg
					})
					this.reset()
					if (this.flag) {
						setTimeout(() => {
							uni.navigateBack(-1)
						}, 1000)
					}
				} else {
					this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			},
			reset() {
				this.query = {
					containerCode: ''
				}
				this.list = []
				this.isolationArea = 0
			},
			async getDetail() {
				const res = await getDetailAPI(this.query)
				if (res.meta.code === 200) {
					this.list = res.data.containerResp.containerDetails.map(item => {
						return {
							...item,
							quantity: null
						}
					})
					this.isolationArea = res.data.containerResp.container.isolationArea
				} else {
					this.list = []
					this.isolationArea = 0
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
.quantity {
	uni-view[data-v-113bc24f] {
		padding: 0 !important;
	}
}
.box-more {
	/deep/ .uni-select {
		background-color: #fff !important;
		padding: 0 !important;
		height: 50rpx !important;
	}
}
</style>
